<?php
#[常用函数管理]
require_once("class/tplfolder.class.php");
$STPL = new QG_C_TPLFOLDER($DB,$prefix);
$tplfolder = $STPL->folder();
$template_id = $STPL->tplid();
define("TPL_FOLDER",$tplfolder);
define("TemplateID",$template_id);
$bi = getfolder(TemplateID);
$_SESSION["tpl_folder"] = $tplfolder;
$_SESSION["template_id"] = $template_id;
unset($STPL);

function Error($msg="操作错误",$url="",$time=2)
{
	global $DB,$system,$langs;
	global $TPL;
	$TPL->set_var("error_msg",$msg);
	$TPL->set_var("error_time",$time);
	$TPL->set_var("error_url",$url);
	$TPL->p("error.sys");
	rewrite();
}

function qgheader($url="404.php")
{
	global $TPL;
	$url = $url ? $url : "404.php";
	ob_end_clean();
	header("Location:".$url);
	exit;
}

function ErrorMsg($msg="操作不正确")
{
	Error($msg);
}

#[检测缓存文件是否超时]
function CheckCache($filename)
{
	global $system;
	global $system_time;
	if(!$filename)
	{
		return false;
	}
	$system["mintime"] = $system["mintime"] ? intval($system["mintime"]) : 0;
	if(!file_exists($filename))
	{
		return false;
	}
	if(!$system["maxtime"] || $system["maxtime"] < 1)
	{
		return false;
	}
	if((@filemtime($filename) + rand($system["mintime"],$system["maxtime"])) <= $system_time)
	{
		return false;
	}
	return true;
}

#[获取指定语言ID的分类信息，这里只读取ID号信息，其他的不读]
function GetCateIdAll($langid)
{
	global $DB,$prefix;
	$rs = $DB->qgGetAll("SELECT id FROM ".$prefix."category WHERE language='".LANGUAGE_ID."'");
	$rsid = array();
	foreach($rs AS $key=>$value)
	{
		$rsid[$value["id"]] = $value["id"];
	}
	return $rsid;
}

#[管理无限级别分类]
function menu_list($catelist,$cateid,$array=array())
{
	foreach($catelist AS $key=>$value)
	{
		if($value["id"] == $cateid)
		{
			$array[$key] = $value;
			$array = menu_list($catelist,$value["parentid"],$array);
		}
	}
	return $array;
}

#[根据当前分类得到子分类ID]
function get_son_list($catelist,$cateid,$array=array())
{
	foreach($catelist AS $key=>$value)
	{
		if($value["parentid"] == $cateid)
		{
			$array[$key] = $value["id"];
			$array = get_son_list($catelist,$value["id"],$array);
		}
	}
	return $array;
}

function page($url,$biao,$total=0,$psize=30,$pageid=0,$halfPage=5)
{
	global $langs,$bi;
	if(empty($psize))
	{
		$psize = 30;
	}
	#[添加链接随机数]
	if(strpos($url,"?") === false)
	{
		$url = $url."?qgrand=phpok";
	}
	#[共有页数]
	$totalPage = intval($total/$psize);
	if($total%$psize)
	{
		$totalPage++;#[判断是否存余，如存，则加一
	}
	#[如果分页总数为1或0时，不显示]
	if($totalPage<2)
	{
		return false;
	}
	#[判断分页ID是否存在]
	if(empty($pageid))
	{
		$pageid = 1;
	}
	#[判断如果分页ID超过总页数时]
	if($pageid > $totalPage)
	{
		$pageid = $totalPage;
	}
	#[Html]
	$array_m = 0;
	if($pageid > 0)
	{
		//$returnlist[$array_m]["url"] = $url;
		//$returnlist[$array_m]["name"] = $langs["page_first".$bi];
		//$returnlist[$array_m]["status"] = 0;
		if($pageid > 1)
		{
			$array_m++;
			$returnlist[$array_m]["url"] = $url."&pageid=".($pageid-1);
			$returnlist[$array_m]["name"] = $langs["prev".$bi];
			$returnlist[$array_m]["classname"] = "page_front";
			$returnlist[$array_m]["status"] = 0;
		}
	}
	#[添加中间项]
	for($i=$pageid-$halfPage,$i>0 || $i=0,$j=$pageid+$halfPage,$j<$totalPage || $j=$totalPage;$i<$j;$i++)
	{
		$l = $i + 1;
		$array_m++;
		$returnlist[$array_m]["url"] = $url."&pageid=".$l;
		$returnlist[$array_m]["name"] = $l;
		$returnlist[$array_m]["classname"] = ($l == $pageid) ? aa : bb;
		$returnlist[$array_m]["status"] = ($l == $pageid) ? 1 : 0;
	}
	#[添加select里的中间项]
	for($i=$pageid-$halfPage*3,$i>0 || $i=0,$j=$pageid+$halfPage*3,$j<$totalPage || $j=$totalPage;$i<$j;$i++)
	{
		$l = $i + 1;
		$select_option_msg = "<option value='".$l."'";
		if($l == $pageid)
		{
			$select_option_msg .= " selected";
		}
		$select_option_msg .= ">".$l."</option>";
		$select_option[] = $select_option_msg;
	}
	#[添加尾项]
	if($pageid < $totalPage)
	{
		$array_m++;
		$returnlist[$array_m]["url"] = $url."&pageid=".($pageid+1);
		$returnlist[$array_m]["name"] = $langs["next".$bi];
		$returnlist[$array_m]["classname"] = "page_back";
		$returnlist[$array_m]["status"] = 0;
	}
	$array_m++;
	//$returnlist[$array_m]["url"] = $url."&pageid=".$totalPage;
	//$returnlist[$array_m]["name"] = $langs["page_last".$bi];
	//$returnlist[$array_m]["status"] = 0;
	#[内容组成html]
	#[组织样式]
	//$msg = "<table class='pagelist'><tr><td class='n'>".$total."/".$psize."</td>";
	$msg = "<table class='pagelist'><tr>";
	foreach($returnlist AS $key=>$value)
	{
		if($value["status"])
		{
			$msg .= "<td><a href='#' class='".$value["classname"]."'>".$value["name"]."</a></td>";
		}
		else
		{
			$msg .= "<td ><a href='".$value["url"]."' class='".$value["classname"]."'>".$value["name"]."</a></td>";
		}
	}
	//$msg .= "<td><select onchange=\"window.location='".$url."&pageid='+this.value+''\">".implode("",$select_option)."</option></select></td>";
	$msg .= "</tr></table>";
	unset($returnlist);
	return $msg;
}





function SafeHtml($msg="")
{
	global $STR;
	return $STR->safe($msg);
}

function FckHtml($msg="",$script=false,$iframe=false,$style=false)
{
	global $STR;
	$STR->set("script",$script);
	$STR->set("iframe",$iframe);
	$STR->set("style",$style);
	return $STR->html($msg);
}

function CutString($string,$length=10,$dot="…")
{
	global $STR;
	return $STR->cut($string,$length,$dot);
}

function Utf2gb($utfstr)
{
	global $STR;
	return $STR->charset($utfstr,"UTF-8","GBK");
}

function GetSystemUrl()
{
	$myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]);
	$docu = $_SERVER["PHP_SELF"];
	$array = explode("/",$docu);
	$count = count($array);
	if($count>1)
	{
		foreach($array AS $key=>$value)
		{
			$value = trim($value);
			if($value)
			{
				if(($key+1) < $count)
				{
					$myurl .= "/".$value;
				}
			}
		}
	}
	$myurl .= "/";
	return $myurl;
}

function FOOT($tplfile)
{
	global $FS,$DB,$prefix;
	global $TPL;
	#[引入页脚信息]
	$TPL->p($tplfile);
	REWRITE();
}

function REWRITE()
{
	global $urlRewrite,$system,$siteurl;
	if($urlRewrite)
	{
		#[定义常量，这些常量是给Rewrite用的]
		define("RW_SITE_URL",$siteurl);
		define("RW_TPL_FOLDER",TPL_FOLDER);
		define("RW_UPFILES","upfiles");
		define("RW_LANG",LANGUAGE_SIGN);
		include_once("class/GetPingYing.class.php");
		include_once("class/rewrite.class.php");
		$RW = new Rewrite();
		$content = ob_get_contents();
		ob_end_clean();
		$content = $RW->qg_rewrite($content);
		echo $content;
		ob_end_flush();
	}
	exit;
}

function ContentPageArray($content,$url,$pageid=0)
{
	$page_content = explode("[:page:]",$content);
	$pageid = intval($pageid);
	if($pageid < 1)
	{
		$pageid = 1;
	}
	$page_total = count($page_content);
	$pagelist = page($url,$bi,$page_total,1,$pageid);#[获取分页的数组]	
	$content = $page_content[($pageid-1)];
	$content = preg_replace("/<div/isU","<p style='margin-top:0px;margin-bottom:0px;padding:0px;'",$content);
	$content = preg_replace("/<\/div>/isU","</p>",$content);
	return array($content,$pagelist);
}

function pass_encry($pass)
{
	$pass = base64_encode(base64_encode($pass));
	$pass_arr = str_split($pass);
	foreach($pass_arr AS $key=>$value)
	{
		if(preg_match('/^[a-z]+$/', $value))
		{
			$value = strtoupper($value);
		}
		elseif(preg_match('/^[A-Z]+$/', $value))
		{
			$value = strtolower($value);
		}
		$pass_new .= $value;
	}
	return $pass_new;
}
function pass_decry($pass)
{
	$pass_arr = str_split($pass);
	foreach($pass_arr AS $key=>$value)
	{
		if(preg_match('/^[a-z]+$/', $value))
		{
			$value = strtoupper($value);
		}
		elseif(preg_match('/^[A-Z]+$/', $value))
		{
			$value = strtolower($value);
		}
		$pass_new .= $value;
	}
	$pass = base64_decode(base64_decode($pass_new));
	return $pass;
}

#[显示语言及模板]
function SelectLangTpl()
{
	global $DB,$prefix,$FS;
	if(file_exists("data/cache/lang_select.php"))
	{
		include_once("data/cache/lang_select.php");
	}
	else
	{
		$langlist = $DB->qgGetAll("SELECT sign,name FROM ".$prefix."lang WHERE ifuse='1' ORDER BY ifdefault DESC,sign ASC,id DESC");
		$FS->qgWrite($langlist,"data/cache/lang_select.php","langlist");
	}
	$select = "<table cellpadding='1' cellspacing='0'><tr><td><select onchange='sys_change_lang(this.value)'>";
	foreach((is_array($langlist) ? $langlist : array()) AS $key=>$value)
	{
		$select .= "<option value='".$value["sign"]."'";
		if($value["sign"] == $_SESSION["qglang"]["sign"])
		{
			$select .= " selected";
		}
		$select .= ">".$value["name"]."</option>";
	}
	$select .= "</select>";
	$select .= "</td><td>";
	$select .= "<select onchange='sys_change_tpl(this.value)'>";
	if(file_exists("data/cache/style_select_".LANGUAGE_ID.".php"))
	{
		include_once("data/cache/style_select_".LANGUAGE_ID.".php");
	}
	else
	{
		$tpl_list = $DB->qgGetAll("SELECT folder,name FROM ".$prefix."tpl ORDER BY isdefault DESC,taxis ASC,id DESC");
		$FS->qgWrite($tpl_list,"data/cache/style_select_".LANGUAGE_ID.".php","tpl_list");
	}
	foreach((is_array($tpl_list) ? $tpl_list : array()) AS $key=>$value)
	{
		$select .= "<option value='".$value["folder"]."'";
		if($value["folder"] == $_SESSION["tpl_folder"])
		{
			$select .= " selected";
		}
		$select .= ">".$value["name"]."</option>";
	}
	$select .= "</select>";
	$select .= "</td></tr></table>";
	$select .= "<span style='display:none;'><script type='text/javascript'>\nfunction sys_change_lang(m){var url='home.php?langsign='+m;window.location.href=url;}\n";
	$select .= "function sys_change_tpl(m){var url='home.php?template='+m;window.location.href=url;}</script></span>";
	$FS->qgWrite($select,"data/cache/lang_style_select.php");
	return $select;
}

#[月份转换]
function monthc($month,$type="min")
{
	global $bi;
	$month = intval($month);
	$cn = array(1=>"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
	$en = array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
	$en_ful = array(1=>"January","February","March","April","May","June","July","August","September","October","November","December");
	if($bi=="_en")
	{
		if($type=="ful"){$array=$en_ful;}
		else{$array=$en;}
	}
	else
	{
		$array=$cn;
	}
	$result = $array[$month];
	return $result;
}

#[获取模板文件夹名]
function getfolder($id)
{
	global $DB,$prefix;
	$rs = $DB->qgGetOne("SELECT folder FROM ".$prefix."tpl WHERE id = ".$id);
	if($rs["folder"]=="default")
	{
		$rs["folder"]="";
	}
	else
	{
		$rs["folder"] = "_".$rs["folder"];
	}
	return $rs["folder"];
}

#[获取ip]
function getipadd()
{
	if(getenv('HTTP_CLIENT_IP'))
	{
		$onlineip = getenv('HTTP_CLIENT_IP'); 
	}
	elseif(getenv('HTTP_X_FORWARDED_FOR'))
	{
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
	}
	elseif(getenv('REMOTE_ADDR'))
	{
		$onlineip = getenv('REMOTE_ADDR');
	}
	else
	{
		$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR']; 
	}
	return $onlineip;
}

//返回语言版本字符
function lan($arr)
{
	echo $arr[BI];
}

#[读取网页内容]
function gethtml($url)
{
	$szUrl = $url;
	$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $szUrl);
	curl_setopt($curl, CURLOPT_HEADER, 0);  //0表示不输出Header，1表示输出
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_ENCODING, '');
	curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	$data = curl_exec($curl); 
	return $data;
	//echo curl_errno($curl); //返回0时表示程序执行成功 如何从curl_errno返回值获取错误信息
	exit();
}

#[去除BOM头部信息]
function clearbom($str)
{
	$result = trim($str,"\xEF\xBB\xBF");
	return $result;
}

#[获取json信息]
function get_jsonhtml($url)
{
	$content = gethtml($url);
	$content = clearbom($content);
	$html = json_decode($content,true);
	return $html;
}

//微信推送信息
function send_wechatmsg($nickname,$email,$tel,$content)
{
	$website = "http://".getServerName();
	$grade=1; //超级管理员==1   普通管理员==2  文员==3
	$link = base64_encode($website);
	$nickname = base64_encode($nickname);
	$email = base64_encode($email);
	$tel = base64_encode($tel);
	$content = base64_encode($content);
	$url="https://www.sy118.com/binding.php?act=sendmsg&link=".$link."&grade=".$grade."&nickname=".$nickname."&email=".$email."&tel=".$tel."&content=".$content;
	header("Location:$url");
}

function get_user_level($id){
    global $DB,$prefix;
    $rs = $DB->qgGetOne("SELECT * FROM ".$prefix."job_cate WHERE id = ".$id);
    return $rs["name"];
}




?>